package com.wyg.service;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wyg.dao.UserDao;
import com.wyg.pojo.User;

public class UpdateService {
	
	public HttpServletRequest request;
	public HttpServletResponse response;
	public UserDao ud = new UserDao();
	
	public UpdateService(HttpServletRequest request, HttpServletResponse response) {
		super();
		this.request = request;
		this.response = response;
	}
	
	public void queryUser(){
		int id =  Integer.parseInt(request.getParameter("id"));
		User user = ud.queryUser(id);
		request.setAttribute("user", user);
		try {
			request.getRequestDispatcher("main/updateUser.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//更改信息
	public void doChange(){
		int id = Integer.parseInt(request.getParameter("id"));
		String phoneNumber = request.getParameter("phone");
		String name = request.getParameter("name");
		String ipid = request.getParameter("ipid");
		String address = request.getParameter("address");
		String abcType = request.getParameter("abcType");
		
		ud.changeUserInfo(id, phoneNumber, name, ipid, address, abcType);
		try {
			request.getRequestDispatcher("servlet?method=allUser").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//重置密码
	public void initPwd(){
		int id = Integer.parseInt(request.getParameter("id"));
		ud.initPwd(id);
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print("success");
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	

}
